var wrongDialog = (text, color) => {
  let dialog = $.confirm({
    title: false,
    type: color,
    content: text,
    closeIcon: false,
    buttons: false
  });
  setTimeout(function () {
    dialog.close();
  }, 1000); // 5000毫秒后关闭弹窗
}
var codeLock = false;
toastr.options = {
  timeOut: 500,
  positionClass: "toast-center-center"
};
var connectAjax = (url, type, data) => {
  return new Promise((resolve, reject) => {
    $.ajax({
      url: url,
      type: type,
      data: data,
      dataType: "json",
      'Content-Type': 'multipart/form-data',
      success: function(result){
        console.log('success');
        resolve(result);
      },
      error: function(error){
        console.log('error');
        reject(error);
      }
    });
  })
}
var verifyCheck = () => {
  if(!$('.agree').is(':checked')){
    wrongDialog('请选择协议', 'red')
    return false;
  } else return true;
}
var verifyAccount = className => {
  let number = $(`.${className}`).val(),
      status = /^1\d{10}$/.test(number), tip = '';
  if (number.trim().length>0) {
    if (!status) {
      wrongDialog('请填写正确的手机号', 'red')
      return false;
    } else {
      return number;
    }
  } else {
    wrongDialog('请填写手机号', 'red')
    return false;
  }
}
var verifyPw = () => {
  // let password = $('.login_password').val(),
  //     regex = new RegExp('^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[!@#$%.^&*]).{8,}$');
  // if (password.trim().length>0) {
  //   if (!regex.test(password)) {
  //     toastr.error('密码至少要包含 数字/字母/符号 中的两种且至少要8位');
  //     return false;
  //   } else {
  //     return password;
  //   }
  // } else {
  //   toastr.error('请输入密码');
  //   return false;
  // }

  let password = $('.login_password').val();
  if (password.trim().length>0) {
    return password;
  } else {
    wrongDialog('请输入密码', 'red')
    return false;
  }
}
var verifyCode = () => {
  let code1 = $('.login_code').val().trim(),
      code2 = $('.login_pcode').val().trim();
  if (code1.length>0 && code2.length>0) {
    return true;
  } else {
    wrongDialog('请输入验证码', 'red')
    return false;
  }
}

$('body')
    .on('click','.ps_login_btn', () =>{
      if (verifyCheck() && verifyAccount('login_account') && verifyPw()) {
        connectAjax('/login', 'post', {
          'mobile': $('.login_account').val(),
          'password': $('.login_password').val(),
          'token': $('.getToken').val()
        }).then(result => {
          if (result.code === 200) {
            // 成功
            window.location.href = `/index`;
          } else {
            wrongDialog(result.message, 'red')
          }
        }).catch(reason => {
          console.log(reason);
        })
      }
    })
    .on('click','.code_login_btn', () =>{
      if (verifyCheck() && verifyAccount('login_phone') && verifyCode()) {
        connectAjax('/login', 'post', {
          'is_sms': 1,
          'mobile': $('.login_phone').val(),
          'code': $('.login_pcode').val(),
          'token': $('.getToken').val()
        }).then(result => {
            if (result.code === 200) {
                // 成功
                window.location.href = `/index`;
            } else {
                wrongDialog(result.message, 'red')
            }
        }).catch(reason => {
          console.log(reason);
        })
      }
    })
    .on('click','.tab',function(){
        $(this).addClass('check').siblings().removeClass('check');
        if($(this).hasClass('account_tab')){
          $('.account_form').fadeIn();
          $('.note_form').hide();
        }else{
          $('.account_form').hide();
          $('.note_form').fadeIn();
        }

    })
    .on('click','.get_code',function(){
        var _this = $(this);
        if(codeLock){
            return;
        }else{
            codeLock = true;
            $.ajax({
                url: '/login/sendMobileCode',
                type: 'post',
                data: {
                  mobile:$('#login_phone').val(),
                  verification_code:$('#login_code').val()
                },
                dataType: 'json',
                success: function(result){
                    if(result.code == 200){
                        var timerNum = 60;
                        _this.text(timerNum);
                        var codeTimer = setInterval(function(){
                            timerNum--;
                            if(timerNum < 0){
                                timerNum = 60;
                                _this.text('发送动态码');
                                codeLock = false;
                                clearInterval(codeTimer);
                            }else{
                                _this.text(timerNum);
                            }
                        },1000);
                    }else if(result.message){
                        wrongDialog(result.message, 'red')
                        codeLock = false;
                    }
                },
                error: function(err){
                    off = false;
                    wrongDialog('网络错误，稍后重试', 'red');
                }
            });


        }
    })
